package leetbook.stack;

import java.util.Stack;

public class Solution150 {

    public static void main(String[] args) {
        String[] token = {"2","1","+","3","*"};
        System.out.println(new Solution150().evalRPN(token));
    }

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String s: tokens) {
            if("+".equals(s)){
                stack.push(stack.pop()+stack.pop());
            }else if("-".equals(s)){
                stack.push(-stack.pop()+stack.pop());
            }else if("*".equals(s)){
                stack.push(stack.pop()*stack.pop());
            }else if("/".equals(s)){
                stack.push((int)(1.0/stack.pop()*stack.pop()));
            }else{
                stack.push(Integer.parseInt(s));
            }
        }
        return stack.pop();
    }
}
